{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paramiko\n",
    "jssh = paramiko.SSHClient()\n",
    "\n",
    "# sftp 文件传输\n",
    "#paramiko.Transport()\n",
    "\n",
    "\n",
    "## 密码登录\n",
    "\n",
    "# 第一次登录是，密码串处理方法\n",
    "# paramiko.AutoAddPolicy() 将登录的密钥信息保存到knonw_hosts文件\n",
    "jssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n",
    "jssh.connect('192.168.1.101', '22', 'test', '123456')\n",
    "\n",
    "\n",
    "## 密钥登录\n",
    "\n",
    "jssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n",
    "# dsa\n",
    "key = paramiko.DSSKey.from_private_key_file('/tmp/id_rsa')\n",
    "#rsa\n",
    "key = paramiko.RSAKey.from_private_key_file('/tmp/id_rsa')\n",
    "jssh.connect('192.168.1.101', '22', pkey=key)\n",
    "\n",
    "stdin, stdout, stderr = jssh.exec_command('ls /tmp')\n",
    "\n",
    "# 获取终端的输出\n",
    "stdout.read()\n",
    "\n",
    "\n",
    "\n",
    "# 命令错误返回\n",
    "stdin, stdout, stderr = jssh.exec_command('lssss /tmp')\n",
    "stderr.read()\n",
    "\n",
    "out: 'bash: lessss: \\xe6\\x9c\\xaa\\xe6\\x89\\xbe\\xe5\\x88\\xb0\\xe5\\x91\\xbd\\xe4\\xbb\\xa4'\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
